<?php

namespace App\Http\Controllers\Api\Pay;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;
use App\Http\Controllers\Api\BaseController;
use App\Repository\AppUser\TouTiaoUserOrderRepository;

class DouYInPayController extends BaseController
{
  public function __construct(TouTiaoUserOrderRepository $TouTiaoUserOrderRepository)
  {
    $this->model = new $TouTiaoUserOrderRepository();
    $this->log = new Logger('PayDouYin');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/PayDouYin.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 抖音那边推送过来的数据  支付回调
  // 接受到支付成功的回调后 然后同步订单
  public function pushOrderStatus(Request $Request)
  {
    $getData = file_get_contents('php://input');
    $this->log->info('input' . ($getData));
    // return json_encode(['code' => 0, 'success' => true]);
    // var_dump(1111111111);
    // $getData = '{"msg":"{\"appid\":\"tt5a272c9ac518d44001\",\"cp_orderno\":\"HK_20230207180048482247961912\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"2023020722001467081431020655\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"PCP2023020718034213171307912292\",\"out_channel_order_no\":\"2023020722001467081431020655\",\"total_amount\":100,\"status\":\"SUCCESS\",\"seller_uid\":\"71665703758060280450\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1675764244,\"message\":\"\",\"order_id\":\"N7197351666734172472\",\"ec_pay_trade_no\":\"NEP2023020718004333138161222292\"}","msg_signature":"17cc4f01273108682f42c808fa8b0b2a5799bed6","nonce":"6604","timestamp":"1675764245","type":"payment"}';
    // var_dump($getData);
    // $getData = json_decode($getData, true);

    // 数据处理  
    if (empty($getData)) {
      return json_encode(['err_no' => 30, 'err_tips' => 'fail']);
    }
    if (!empty($getData) && is_string($getData)) {
      $getData = json_decode($getData, true);
    }

    if (empty($getData)) {
      return json_encode(['err_no' => 20, 'err_tips' => 'fail']);
    }
    if (!empty($getData['msg']) && is_string($getData['msg'])) {
      $getData['msg'] = json_decode($getData['msg'], true);
    }
    if (empty($getData['msg']['cp_orderno']) && empty($getData['msg']['cp_refundno'])) {
      return json_encode(['err_no' => 10, 'err_tips' => 'fail']);
    }
    if (!empty($getData['msg']['cp_orderno'])) {
      $where = [
        'orderno' => $getData['msg']['cp_orderno'],
      ];
      $update = [
        'status' => 2
      ];
    }
    if (!empty(($getData['msg']['cp_refundno']))) {
      $where = [
        'orderno' => $getData['msg']['cp_refundno'],
      ];
      $update = [
        'status' => 6
      ];
    }
    if (empty($where) && empty($update)) {
      return json_encode(['err_no' => 1000, 'err_tips' => 'fail']);
    }
    $this->model->edit($where, $update);
    // 给抖音 同步订单
    return json_encode(['err_no' => 0, 'err_tips' => 'success']);
    return 'error';
  }
}
